package com.lijian.controller;

import com.lijian.entity.Book;
import com.lijian.entity.BookPage;
import com.lijian.service.BookService;
import net.bytebuddy.implementation.bind.annotation.Origin;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Book)表控制层
 *
 * @author makejava
 * @since 2020-04-11 20:52:07
 */
@RestController
@RequestMapping("book")
public class BookController {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Book selectOne(Integer id) {
        return this.bookService.queryById(id);
    }

    @GetMapping("/list/{page}/{size}")
    public BookPage findList(@PathVariable Integer page, @PathVariable Integer size) {
        Integer offset = (page - 1) * size;
        if(offset <= 0){
            offset = 0;
        }
        return bookService.queryAllByLimit(offset, size);
    }

    @PostMapping("/save")
    public String save(@RequestBody Book book){
        Book result = bookService.insert(book);
        if(result != null){
            return "success";
        }
        return "error";
    }

    @GetMapping("/findById/{id}")
    public Book findById(@PathVariable Integer id){
        return bookService.queryById(id);
    }

    @PutMapping("/update")
    public String update(@RequestBody Book book){
        Book result = bookService.update(book);

        if(result != null){
            return "success";
        }
        return "error";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id){
        boolean result = bookService.deleteById(id);
        if(result){
            return "success";
        }
        return "error";
    }

}